/**
 * 
 */
package com.dalonedrau.graph;

import com.dalonedrau.vectors.Vector2;

/**
 * @author Donald
 *
 */
public class River {
	private RiverNode[] nodes;
	private int flow;
	/**
	 * Gets the flow 
	 * @return {@link int}
	 */
	public int getFlow() {
		return flow;
	}
	/**
	 * Sets the flow
	 * @param flow the flow to set
	 */
	public void setFlow(int flow) {
		this.flow = flow;
	}
	/**
	 * Creates a new instance of {@link River}.
	 */
	public River() {
		nodes = new RiverNode[0];
	}
	/**
	 * Gets the nodes 
	 * @return {@link RiverNode[]}
	 */
	public RiverNode[] getNodes() {
		return nodes;
	}
	public RiverNode getNode(final int index) {
		RiverNode node = null;
		for (int i = 0; i < nodes.length; i++) {
			if (nodes[i].getIndex() == index) {
				node = nodes[i];
			}
		}
		return node;
	}
	public RiverNode getNode(final int x, final int y) {
		RiverNode node = null;
		for (int i = 0; i < nodes.length; i++) {
			if (nodes[i].equals(x, y)) {
				node = nodes[i];
			}
		}
		return node;
	}
	/**
	 * Adds a node to the graph.
	 * @param node the {@link GraphNode} to be added
	 * @throws Exception if a node has already been added with the same index.
	 */
	public void addNode(final RiverNode node) throws Exception {
		if (contains(node.getIndex())
				&& nodes[node.getIndex()].equals(node)) {
			throw new Exception("node " + node.getIndex()
					+ " already added to graph.");
		}
		if (nodes.length <= node.getIndex()) {
			// extend the length of the nodes array
			RiverNode[] dest = new RiverNode[node.getIndex() + 1];
			System.arraycopy(nodes, 0, dest, 0, nodes.length);
			nodes = dest;
			dest = null;
		}
		nodes[node.getIndex()] = node;
	}
	/**
	 * Determines if a node has been added to the graph at the given index.
	 * @param index the specific index
	 * @return true if the graph contains a node at the given index; false
	 *         otherwise
	 */
	public boolean contains(final int index) {
		boolean contains = false;
		if (index < nodes.length
				&& nodes[index] != null) {
			contains = true;
		}
		return contains;
	}
	/**
	 * Determines if a node has been added to the graph that matches the given 
	 * vector location.
	 * @param x the vector's x-position
	 * @param y the vector's y-position
	 * @return true if the graph contains a node matching the specific vector;
	 * false otherwise
	 */
	public boolean contains(final double x, final double y) {
		boolean contains = false;
		for (int i = 0; i < nodes.length; i++) {
			if (nodes[i].equals(x, y)) {
				contains = true;
				break;
			}
		}
		return contains;
	}
	/**
	 * Determines if a node has been added to the graph that matches the given 
	 * vector.
	 * @param v2 the vector
	 * @return true if the graph contains a node matching the specific vector;
	 * false otherwise
	 */
	public boolean contains(final Vector2 v2) {
		boolean contains = false;
		for (int i = 0; i < nodes.length; i++) {
			if (nodes[i].equals(v2)) {
				contains = true;
				break;
			}
		}
		return contains;
	}
}
